package com.iplus.ydkf.ehome.module.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.iplus.ydkf.ehome.R;
import com.iplus.ydkf.ehome.common.utils.RegexValidateUtil;
import com.iplus.ydkf.ehome.module.base.BaseActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by lhtde on 2016/4/20.
 */
public class ForgetPwdActivity extends BaseActivity {
    @Bind(R.id.et_forget_username)
    EditText et_forget_username;

    @Override
    public int bindLayout() {
        return R.layout.acitivity_forgetpwd;
    }

    @OnClick(R.id.btn_forgetpwd)
    public void forgetpwd() {
        if (isEmptyInput())
            return;
        AVQuery<AVUser> userAVQuery = AVUser.getQuery();
        userAVQuery.whereEqualTo("mobilePhoneNumber", et_forget_username.getText().toString());
        userAVQuery.findInBackground(new FindCallback<AVUser>() {
            @Override
            public void done(List<AVUser> list, AVException e) {
                if (isNoException(e))
                    if (list.size() == 0)
                        snackbar("该号码尚未注册！");
                    else {
                        Intent intent = new Intent(ForgetPwdActivity.this, RestPasswordActivity.class);
                        intent.putExtra("phoneNumber", et_forget_username.getText().toString());
                        startActivity(intent);
                    }
            }
        });
    }

    private boolean isEmptyInput() {
        if (TextUtils.isEmpty(et_forget_username.getText().toString())) {
            snackbar("手机号不能为空");
            return true;
        }
        if (!RegexValidateUtil.checkCellphone(et_forget_username.getText().toString())) {
            snackbar("请输入正确的手机号");
            return true;
        }
        return false;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void initData(View view) {

    }

    @Override
    public String getFragmentName() {
        return null;
    }

}
